package com.hxkj.service.impl;

import com.hxkj.client.service.AuthUserServiceClient;
import com.hxkj.client.service.SiteServiceClient;
import com.hxkj.domain.enums.SiteExceptionEnum;
import com.hxkj.domain.enums.UserExceptionEnum;
import com.hxkj.domain.po.AllocationSite;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.domain.po.Site;
import com.hxkj.service.SiteService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 站点服务
 * <p>
 * Create by chenwei on 2018/5/23 15:19
 */
@Service
@Slf4j
public class SiteServiceImpl implements SiteService {

    @Autowired
    private SiteServiceClient siteServiceClient;

    @Autowired
    private AuthUserServiceClient authUserServiceClient;
    @Autowired
    private TokenUtils tokenUtils;


    @Override
    public ResultPageStruct<Site> getSiteList(Site site, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ResultPageStruct<Site>  result = new ResultPageStruct<>();
        ParameterDataStruct<Site> parameterDataStruct = new ParameterDataStruct<>();
        AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
        site.setUserId(authUser.getUserId());
        parameterDataStruct.setData(site);
        parameterDataStruct.setPageTo(pageParam);
        try {
            // 查询站点列表
            result= siteServiceClient.getList(parameterDataStruct);
        }catch (Exception e){
            e.printStackTrace();
            log.error("获取站点信息列表失败:{}",e.getMessage());

        }
        result.setCode(null);
        result.setMessage(null);
        return result;
    }

    @Override
    public List<Site> getSwitcherList(Site site) throws Exception {
        ResultDataStruct<List<Site>>   result = new ResultDataStruct();
        AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
        site.setUserId(authUser.getUserId());
        try {
            // 查询切换站点列表
            result = siteServiceClient.getSwitcherList(site);
            if(result.getCode()!=200){
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询切换站点列表失败:{}",e);
            throw e;
        }
        return result.getData();
    }


    @Override
    public Site getSiteFromCache(Site site) throws Exception {
        ResultDataStruct<Site>   result = new ResultDataStruct();
        if(site.getSiteId()==null){
            throw new CustomException(SiteExceptionEnum.SITE_NOT_ID);
        }

        try {
            // 查询单条站点
            result = siteServiceClient.getSite(site);
            if(result.getCode()!=200){
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询单条站点失败:{}",e);
            throw e;
        }
        return result.getData();
    }

    @Override
    public ResultMessageStruct updateSite(Site site) throws Exception {
        if (site.getSiteId() == null) {
            throw new CustomException(SiteExceptionEnum.SITE_NOT_ID);
        }
        if (StringUtils.isBlank(site.getSiteName()) ) {
            throw new CustomException(SiteExceptionEnum.SITE_NOT_NAME);
        }
        ResultMessageStruct  result;
        try {
            result = siteServiceClient.updateSite(site);
            if(result.getCode()!=200){
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public ResultMessageStruct deleteSite(Site site)  throws Exception {
        ResultMessageStruct   result = new ResultMessageStruct();
        if (StringUtils.isBlank(site.getSiteIds()) ) {
            throw new CustomException(SiteExceptionEnum.SITE_NOT_ID);
        }
        try {
            // 删除站点
            result = siteServiceClient.deleteSite(site);
            if(result.getCode()!=200){
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public ResultMessageStruct saveSite(Site site) throws Exception {
        ResultMessageStruct   result = new ResultMessageStruct();
        if (StringUtils.isBlank(site.getSiteName()) ) {
            // 传入的站点名称为空
            throw new CustomException(SiteExceptionEnum.SITE_NOT_NAME);
        }
        if (site.getSiteStatus()==null ) {
            // 传入的站点状态为空
            throw new CustomException(SiteExceptionEnum.SITE_SITESTATUS_NULL);
        }
        try {
            // 新增站点
            result = siteServiceClient.saveSite(site);
            if(result.getCode()!=200){
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            log.error("新增站点信息失败:{}", e);
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public Site getSiteRefresh(Long siteId) throws Exception {
        return null;
    }

    @Override
    public ResultMessageStruct saveUserSite(AuthUser authUser) throws Exception {

        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            //Mr.zheng:为用户分配站点为空的情况下默认分配一个平台站点ID
            if (authUser.getSiteIds().equals("") || null == authUser.getSiteIds()){
                authUser.setSiteIds("0");
            }
            //Mr.zheng:子站无法分配站点
            if (authUser.getSiteId()!=0){
                throw new CustomException(UserExceptionEnum.SUBSITE_NOT_SITE);
            }
            //Mr.zheng:判断用户ID是否为空，诺为空即用户不存在
            if (authUser.getUserId() == null) {
                resultMessageStruct.setCode(UserExceptionEnum.USER_NOT_UID.getCode());
                resultMessageStruct.setMessage(UserExceptionEnum.USER_NOT_UID.getMsg());
                return  resultMessageStruct;
            }
            //Mr.zheng:判断用户是否为超级管理员，如果是则无法添加站点
            if (authUser.getUserId() == 1) {
                resultMessageStruct.setCode(UserExceptionEnum.ADMIN_NOT_SITE.getCode());
                resultMessageStruct.setMessage(UserExceptionEnum.ADMIN_NOT_SITE.getMsg());
                return resultMessageStruct;
            }
            resultMessageStruct = authUserServiceClient.saveUserSite(authUser);
            if (resultMessageStruct.getCode() == 200) {
                return resultMessageStruct;
            } else {
                throw new CustomException(UserExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("添加用户站点信息失败:{}",e.getMessage());
            throw e;
        }
    }

    @Override
    public AllocationSite getUserSiteList(AuthUser authUser) throws Exception {
         ResultDataStruct<AllocationSite> resultDataStruct = new ResultDataStruct<>();
        AuthUser auser = (AuthUser) tokenUtils.getTokenUserInfo();
        //Mr.zheng:子站无法分配站点
        if (authUser.getSiteId()!=0){
            throw new CustomException(UserExceptionEnum.SUBSITE_NOT_SITE);
        }
        //Mr.zheng:不是超级管理员无法分配站点
        if (auser.getUserId()!= 1){
            throw new CustomException(UserExceptionEnum.NO_ADMIN_NOT_SITE);
        }
        //Mr.zheng:判断用户ID是否为空

        if (authUser.getUserId() == null) {
            resultDataStruct.setCode(UserExceptionEnum.USER_NOT_UID.getCode());
            resultDataStruct.setMessage(UserExceptionEnum.USER_NOT_UID.getMsg());
            throw new CustomException(UserExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
        }
        if (authUser.getUserId() == 1) {
            throw new CustomException(UserExceptionEnum.ADMIN_NOT_SITE);
        }

            resultDataStruct = authUserServiceClient.getUserSite(authUser);
            if (resultDataStruct.getCode() == 200) {
                return resultDataStruct.getData();
            } else {
                throw new CustomMessageException(resultDataStruct.getCode(),resultDataStruct.getMessage());
            }

    }

    @Override
    public ResultMessageStruct SiteSort(Site site) throws Exception {
        ResultMessageStruct   result = new ResultMessageStruct();
        try {
            // 排序站点
            result = siteServiceClient.SiteSort(site);
            if(result.getCode()!=200){
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
